package cn.edu.xcu.ckl.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.edu.xcu.ckl.R;
import cn.edu.xcu.ckl.entity.ProductInfo;

public class LeftListAdapter extends RecyclerView.Adapter<LeftListAdapter.MyHolder> {

    private List<ProductInfo> dataList = new ArrayList<>();

    public List<ProductInfo> getDataList() {
        return dataList;
    }

    public void setDataList(List<ProductInfo> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    private int currentIndex=0;


    @NonNull
    @Override

    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.left_list_item, null);
        return new MyHolder(view);
    }

    @Override
    @SuppressLint("RecyclerView")
    public void onBindViewHolder(@NonNull MyHolder holder,  int position) {

        //绑定数据
        ProductInfo  productInfo= dataList.get(position);
        holder.tv_name.setText(productInfo.getProduct_title());

        //分类的点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mLeftListOnClickItemListener) {
                    mLeftListOnClickItemListener.onItemClick(productInfo,position);
                }
            }
        });

        if (currentIndex ==position){
            holder.itemView.setBackgroundResource(R.drawable.type_selector_bg);
        }else {
            holder.itemView.setBackgroundResource(R.drawable.type_selector_normal_bg);
        }


    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder {
        TextView tv_name;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.name);
        }
    }


    private LeftListOnClickItemListener mLeftListOnClickItemListener;


    public void setLeftListOnClickItemListener(LeftListOnClickItemListener leftListOnClickItemListener) {
        mLeftListOnClickItemListener = leftListOnClickItemListener;
    }

    public interface LeftListOnClickItemListener {
        void onItemClick(ProductInfo productInfo, int position);
    }


    public void setCurrentIndex(int position){
        this.currentIndex =position;
        //这句话不能少
        notifyDataSetChanged();
    }
}
